package com.housediyadmin.service
{
	import com.housediyadmin.events.newsEvents.GetNewsEvent;
	import com.housediyadmin.vo.News;
	
	import mx.collections.ArrayCollection;
	import mx.controls.Alert;
	import mx.messaging.ChannelSet;
	import mx.messaging.channels.AMFChannel;
	import mx.rpc.events.FaultEvent;
	import mx.rpc.events.ResultEvent;
	import mx.rpc.remoting.RemoteObject;
	
	import org.robotlegs.mvcs.Actor;
	
	/**
	 * 
	 * @author yanghanbing
	 * 2010/12/16
	 */	
	public class NewsRemoteObjectService extends Actor
	{
		public var newsRemoteObject:RemoteObject;
		
		public function NewsRemoteObjectService()
		{
			super();
			
			var amfChannel:Array = [new AMFChannel(null, "http://localhost/housediyadmin/Gateway.aspx")];
			//var amfChannel:Array = [new AMFChannel(null, "http://10.60.1.120/HouseDiyAdmin/Gateway.aspx")];
			var channelSet:ChannelSet = new ChannelSet();
			channelSet.channels = amfChannel;
			
			newsRemoteObject = new RemoteObject("fluorine");
			newsRemoteObject.source = "HouseDiyService.HouseDiyAction";
			newsRemoteObject.showBusyCursor = true;
			newsRemoteObject.channelSet = channelSet;
			
			/*newsRemoteObject.getOperation("getAllNews").addEventListener(ResultEvent.RESULT, getAllNewsResult);
			newsRemoteObject.getOperation("getAllNews").addEventListener(FaultEvent.FAULT, faultHandler);
			
			newsRemoteObject.getOperation("deleteNews").addEventListener(ResultEvent.RESULT, deleteNewsResult);
			newsRemoteObject.getOperation("deleteNews").addEventListener(FaultEvent.FAULT, faultHandler);
			
			newsRemoteObject.getOperation("addNews").addEventListener(ResultEvent.RESULT, addNewsResult);
			newsRemoteObject.getOperation("addNews").addEventListener(FaultEvent.FAULT, faultHandler);
			
			newsRemoteObject.getOperation("editNews").addEventListener(ResultEvent.RESULT, editNewsResult);
			newsRemoteObject.getOperation("editNews").addEventListener(FaultEvent.FAULT, faultHandler);*/
		}
		
		public function getNewsList():void
		{
			newsRemoteObject.getAllNews();
		}
		
		public function deleteNewsByIds(newsIds:Array):void
		{
			newsRemoteObject.deleteNews(newsIds);	
		}
		
		public function addOneNews(news:News, goodsIds:Array = null):void
		{
			newsRemoteObject.addNews(news, goodsIds);
		}
		
		public function editOneNews(news:News, goodsIds:Array = null):void
		{
			newsRemoteObject.editNews(news, goodsIds);
		}
		
		private function getAllNewsResult(event:ResultEvent):void
		{
			dispatch(new GetNewsEvent(GetNewsEvent.UPDATE_NEWS_MODEL, event.result as ArrayCollection));
		}
		
		private function deleteNewsResult(event:ResultEvent):void
		{
			dispatch(new GetNewsEvent(GetNewsEvent.UPDATE_NEWS_MODEL, event.result as ArrayCollection));
		}
		
		private function addNewsResult(event:ResultEvent):void
		{
			dispatch(new GetNewsEvent(GetNewsEvent.UPDATE_NEWS_MODEL, event.result as ArrayCollection));
		}
		
		private function editNewsResult(event:ResultEvent):void
		{
			dispatch(new GetNewsEvent(GetNewsEvent.UPDATE_NEWS_MODEL, event.result as ArrayCollection));
		}
		
		private function faultHandler(event:FaultEvent):void
		{
			Alert.show(" fault :" + event.toString());
		}
		
		
	}
}